<template>
    <PageLayout :title="title" >
        <view class="contanier">
            <view class="content">
                <view class="title"> {{ message.sender_desc }} 邀请您加入项目</view>
                <view>
                    <view class="avatar-box">
                        <view class="avatar-list">
                            <u-avatar v-for="(user,idx) in project.peopleList" :key="'user-'+idx" :src="user.avatarUrl" :size="avatarSize"></u-avatar>
                        </view>
                    </view>
                    <view>{{project.name}} （{{project.peopleCnt}}人）</view>
                </view>
                <view class="btn-box">
                    <up-button type="success" @click="handlerJoin">点击加入</up-button>
                </view>
            </view>
        </view>
    </PageLayout>
</template>


<script lang="ts" setup>
    import PageLayout from '../../components/myLayout/PageLayout.vue';
    import {ref,getCurrentInstance,computed} from "vue"
    import {onLoad} from "@dcloudio/uni-app"
    import { ReadMessageReq } from '../../http/projectMessage';
    import { GetInviteDataReq, ProjectGetReq, ProjectJoinReq } from '../../http/project';
    import { MyAlert } from '../../utils/utils';
    import { useProjectStore } from '../../store/projectStore';

    const title =ref("邀请你进入项目")
    
    const avatarUrl=ref("https://uview-plus.jiangruyi.com/h5/static/uview/album/2.jpg")

    const instance=getCurrentInstance()?.proxy

    const project=ref({})

    const projectStore=useProjectStore()

    const message=ref({})

    const avatarSize=computed(()=>{
        if(project.value.peopleCnt>=3){
            return 60
        }else{
            return 180/project.value.peopleCnt
        }
    })

    const handlerJoin=()=>{
        ProjectJoinReq({pid:project.value.id}).then(res=>{
            MyAlert({title:"加入成功",content:"是否进入项目页面？"}).then(async ()=>{
                const res=await ProjectGetReq({id:project.value.id})
				await projectStore.setProject(res.data)
                uni.redirectTo({
					url:"/pages/aaProject/index?id="+project.value.id
				})
            }).catch(()=>{
                uni.navigateBack()
            })
        })
    }

    const eventChannel=instance.getOpenerEventChannel()

    eventChannel.on("readMessage",(res)=>{
        ReadMessageReq({mid:res.messageId}).then(res=>{
            message.value=res.data
        })
    })
    
    onLoad((options) => {
        console.log(options)
        GetInviteDataReq({pid:options.pid}).then(res=>{
            project.value=res.data
        })
    })


    

</script>

<style lang="scss" scoped>
    .contanier{
        text-align: center;
        .content{
            width: 80%;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            align-items: center;
            .title{
                font-size: 30rpx;
                font-weight: bold;
                margin: 50upx 0;
            }
        }
        
    }
    .avatar-box{
        display: inline-block;
    }
    .avatar-list{
        width: 400upx;
        display: flex;
        height: 400upx;
        background: #fff;
        flex-wrap: wrap;
        border-radius: 40upx;
        margin-bottom: 30upx;
        align-items: center;
        justify-content: center;
        ::v-deep .u-avatar{
            margin: 11upx;
        }
    }
    .btn-box{
        margin-top: 70upx;
        width: 50%;
    }
</style>